package simulations.factory;

import simulations.Entity;
import springs.AbstractSpring;
import springs.Bar;
import springs.Muscle;
import springs.Spring;

import java.util.Scanner;

/**
 * Factory for creating Springs from a model file
 * @author Zach Michaelov
 */
public class SpringFactory {
    /**
     * AbstractSpring factory method
     *
     * @param line the line from the model file
     * @param type
     * @return the AbstractSpring described by line
     */
    public static AbstractSpring fromScanner(Scanner line, Entity entity, String type) {

        int m1 = line.nextInt();
        int m2 = line.nextInt();
        double restLength = line.nextDouble();
        double ks = line.nextDouble();
        AbstractSpring newSpring = null;

        if (type.equals("spring")) {
            if (ks < 0) { // if k is negative, then bar
                newSpring = new Bar(entity.getMass(m1), entity.getMass(m2), ks);
            } else {
                newSpring = new Spring(entity.getMass(m1), entity.getMass(m2), restLength, ks);
            }
        } else if (type.equals("muscle")) {
            double amplitude = line.nextDouble();
            newSpring = new Muscle(entity.getMass(m1), entity.getMass(m2), restLength, ks, amplitude);
        }
        return newSpring;
    }

}
